package com.bl.weilan.manager.controller.companyopponent;

import com.bl.weilan.common.ApiResponseBody;
import com.bl.weilan.dao.model.CompanyOpponentEntity;
import com.bl.weilan.manager.controller.BaseController;
import com.bl.weilan.service.biz.companyopponent.CompanyOpponentService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.companyopponent.CompanyOpponentAddVo;
import com.bl.weilan.service.vo.companyopponent.CompanyOpponentListVo;
import com.bl.weilan.service.vo.companyopponent.CompanyOpponentModVo;
import com.bl.weilan.service.vo.traded.TradeDDelBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@Log4j2
@RequestMapping(value = {"/weilan/manager/opponent"})
@Api(value = "竞争对手相关", description = "竞争对手相关api")
public class CompanyOpponentController extends BaseController {
    @Autowired
    CompanyOpponentService companyopponentService;

    @ApiOperation(value = "获取某个四级目录的竞争对手列表信息", notes = "获取某一4级分类下的竞争对手列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "company", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "tradeDId", value = "4级分类", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = false, paramType = "query", dataType = "int", defaultValue = "10")})
    @GetMapping(value = {"/get_list"})
    public ApiResponseBody<CompanyOpponentListVo> companyopponentList(
            @RequestParam(value = "companyId", required = false) String companyId,
            @RequestParam(value = "tradeDId", required = false) String tradeDId,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);

        CompanyOpponentListVo companyopponentListVo = companyopponentService.getCompanyOpponentLists(companyId, tradeDId,pageable);

        return ApiResponseBody.createSuccessBody(companyopponentListVo);
    }

    @ApiOperation(value = "获取一条竞争对手详情信息", notes = "获取一条竞争对手")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_detail"})
    public ApiResponseBody<CompanyOpponentListVo> companyopponentList(
            @RequestParam(value = "id", required = true) String id,
            HttpServletRequest request) {

        CompanyOpponentEntity companyopponentEntity = companyopponentService.getCompanyOpponentById(id);

        return ApiResponseBody.createSuccessBody(companyopponentEntity);
    }

    @ApiOperation(value = "增加竞争对手信息", notes = "增加竞争对手信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add"})
    public ApiResponseBody add(
            @Valid @RequestBody CompanyOpponentAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        companyopponentService.addCompanyOpponent(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "删除批量", notes = "删除批量 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del_list"})
    public ApiResponseBody delList(
            @Valid @RequestBody DelListBody body, BindingResult result,
            HttpServletRequest request) {

        companyopponentService.delList(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "删除一条竞争对手信息", notes = "删除单个竞争对手信息 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del"})
    public ApiResponseBody del(
            @Valid @RequestBody TradeDDelBody body, BindingResult result,
            HttpServletRequest request) {


        companyopponentService.del(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "修改竞争对手信息", notes = "修改竞争对手信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/modify"})
    public ApiResponseBody modify(
            @Valid @RequestBody CompanyOpponentModVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        companyopponentService.modify(body);

        return ApiResponseBody.createSuccessBody(null);
    }
}
